package com.cn.qtms.granter.sms;

import com.cn.qtms.granter.AbstractCustomTokenGranter;
import com.cn.qtms.service.JpaSmsCodeServiceImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.Map;

/**
 * @author User
 */
public class SmsCodeTokenGranter extends AbstractCustomTokenGranter {
    /**
     * 登陆类型
     */
    private static final String GRANT_TYPE="smsCode";
    private JpaSmsCodeServiceImpl smsCodeService;

    public SmsCodeTokenGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory, JpaSmsCodeServiceImpl userDetailsService ) {
        super(tokenServices, clientDetailsService, requestFactory, GRANT_TYPE);
        this.smsCodeService=userDetailsService;
    }

    @Override
    protected UserDetails getUserDetails(Map<String, String> parameters) {
        String phone=parameters.get("phone").toString();
        String smsCode=parameters.get("sms_code");
        return smsCodeService.loadUserBySmsCode(phone,smsCode);
    }

}